<?php

declare(strict_types=1);

namespace App\Exchange\Process;

use App\Exchange\Job\SendTradeKlineJob;
use Hyperf\Process\AbstractProcess;
use Hyperf\Process\Annotation\Process;

#[Process(name: 'SubscribeTradeProcess')]
class SubscribeTradeProcess extends AbstractProcess
{

    public function handle(): void
    {
        $pair = redis()->hGetAll('pair');
        $channel = [];
        $interval = ['1s', '1m', '5m', '15m', '30m', '1h', '4h', '12h', '1d', '1w', '1M'];
        foreach ($pair as $v) {
            foreach (explode(',', strtolower($v)) as $symbol) {
                foreach ($interval as $in) $channel[] = 'kline_' . $symbol . '_' . $in;
            }
        }

        redis()->subscribe($channel, function ($redis, $channel, $message) {
            fastapp_queue_push(new SendTradeKlineJob($message, 'trade_kline', explode('_', $channel)[1]));
        });
    }


    public function isEnable($server): bool
    {
        return false;
        return env('APP_ENV') == 'prod';
    }
}
